<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use \DB;

class Statistic extends Model
{
    //

    protected $table = 'statistic';


    public $timestamps = false;


    /**
	 * @param string $field 需要更新的字段
	 * 
	 */
	
	static function updateStatistic($field = ''){
		
		$date_data = array(
				
			'day'=> date('Ymd'),
			'month'=> date('Ym'),
			'year' => date('Y')
			
		);
		
		if($field){
			$field = strtolower($field);
			
			foreach ($date_data as $key => $value){
				
				$exist_info = self::where($key, '=', $value)->first();
				if(!empty($exist_info) && $exist_info->id) {
					self::where($key, '=', $value)->update([$field => $exist_info->$field + 1]);
				}else{
					DB::table('statistic')->insert(
					[$key => $value, $field => 1]
					);
				}
				
			}
		}
	}

}
